//
// Created by wangxiyue on 2019/8/12.
//
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <inaddr.h>
#include <io.h>

int start_client() {

    // --- windows中创建socket需要先注册 --- start
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD(1, 1);
    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) {
        printf(("\n\n-----TCP WSAStartup failed------\n\n"));
        return S_FALSE;
    }
    // windows 创建socket需要添加的代码 ---end

    struct sockaddr_in name;


    int  sock = socket(PF_INET,SOCK_STREAM,0);

    if(sock < 0){
        perror("socket异常");
        exit(0);
    }


    name.sin_family = AF_INET;
//    name.sin_port =
    name.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    int bi = bind(sock, (struct sockaddr *)&name, sizeof(name));
    if (bi < 0) {
        perror("bind error");
        exit(EXIT_FAILURE);
    }

    struct sockaddr_in server_addr;
    int conn = connect(sock, &server_addr, sizeof(server_addr));

    if(conn > -1){
        readn(sock,)
    }



}

/* 从 socketfd 描述字中读取 "size" 个字节. */
ssize_t readn(int fd, void *vptr, size_t size)
{
    size_t  nleft;
    ssize_t nread;
    char    *ptr;

    ptr = vptr;
    nleft = size;

    while (nleft > 0) {
        if ( (nread = read(fd, ptr, nleft)) < 0) {
            if (errno == EINTR)
                nread = 0;      /* 这里需要再次调用 read */
            else
                return(-1);
        } else if (nread == 0)
            break;              /* EOF(End of File) 表示套接字关闭 */


        nleft -= nread;
        ptr   += nread;
    }
    return(n - nleft);      /* 返回的是实际读取的字节数 */
}


int main() {
    start_client();
}